//
// checkStack.cpp for  in /home/jousse_f//workspace/C++/abstractVM
// 
// Made by florian jousseau
// Login   <jousse_f@epitech.net>
// 
// Started on  Tue Feb 14 14:57:54 2012 florian jousseau
// Last update Thu Feb 16 19:23:12 2012 florian jousseau
//

#include <cmath>
#include <climits>
#include <cfloat>
#include "Exception.hh"
#include "checkStack.hh"

#define	MIN	-10000000000000000000000000000.0

bool			checkStack(size_t size, const std::list<IOperand *> &s)
{
  if (s.size() >= size)
    return true;
  return false;
}


void			checkFlow(eOperandType type, double nb)
{
  if (type == INT8)
    {
      if (nb < SCHAR_MIN)
	throw AbstractEx("[Error] Underflow int8");
      if (nb > SCHAR_MAX)
	throw AbstractEx("[Error] Overflow int8");
    }
  if (type == INT16)
    {
      if (nb < SHRT_MIN)
	throw AbstractEx("[Error] Underflow int16");
      if (nb > SHRT_MAX)
	throw AbstractEx("[Error] Overflow int16");
    }
  if (type == INT32)
    {
      if (nb < LONG_MIN)
	throw AbstractEx("[Error] Underflow int32");
      if (nb > LONG_MAX)
	throw AbstractEx("[Error] Overflow int32");
    }
  if (type == FLOAT)
    {
      if (nb < MIN)
	throw AbstractEx("[Error] Underflow Float");
      if (nb > pow(3.4, 38))
	throw AbstractEx("[Error] Overflow Float");
    }
  if (type == DOUBLE)
    {
      if (nb < MIN)
	throw AbstractEx("[Error] Underflow Double");
      if (nb > pow(17.98, 308))
	throw AbstractEx("[Error] Overflow Double");
    }
}

bool			checkDivZero(double nb)
{
  if (nb == 0)
    return (false);
  return (true);
}

eOperandType		findType(const IOperand *nb1, const IOperand *nb2)
{
  if (nb1->getType() > nb2->getType())
    return (nb1->getType());
  else
    return (nb2->getType());
}

std::string		checkValue(std::string & value)
{
  int	i = value.find(".");
  if (i == -1)
    value += ".0";
  return (value);
}

t_value	tab_val[] =
  {
    {"int8", INT8},
    {"int16", INT16},
    {"int32", INT32},
    {"float", FLOAT},
    {"double", DOUBLE},
    {"", NOTHING},
  };

eOperandType		selectType(const std::string &T)
{
  int	i = 0;

  while (tab_val[i].str != T && (tab_val[i].str != ""))
    i++;
  return (tab_val[i].type);
}
